﻿using LargeFilesSample.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;


/* 【MySQL 表结构及测试数据如下】

DROP TABLE IF EXISTS `book_list`;
CREATE TABLE `book_list`  (
  `id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
  `title` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '书名',
  `author` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '作者',
  `isbn_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'ISBN代码',
  `published_by` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '出版社名称',
  `publish_date` date NOT NULL COMMENT '出版年月日',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB COMMENT = '馆藏书名列表' ROW_FORMAT = Compact;

INSERT INTO `book_list` VALUES (1, '西游记', '吴承恩', '123-12345678', '青岛出版社', '2023-01-01');
INSERT INTO `book_list` VALUES (2, '水浒传', '施耐庵', '456-4567890', '青岛出版社', '2023-01-02');
INSERT INTO `book_list` VALUES (3, '红楼梦', '曹雪芹', '321-1234567', 'xx出版社', '2020-01-01');
INSERT INTO `book_list` VALUES (4, '三国演义', '罗贯中', '999-8888-6666', 'xx出版社', '2023-01-03');
INSERT INTO `book_list` VALUES (5, '张三自传', '张三', '978-123456789-0', 'xx出版社', '2012-01-01');
INSERT INTO `book_list` VALUES (6, '李四自传', '李四', '123-2222-44444', 'yy出版社', '2023-01-04');
INSERT INTO `book_list` VALUES (7, 'xx', 'xx', 'xx', 'xx', '2023-01-03');
INSERT INTO `book_list` VALUES (8, 'yy', 'yy', 'yy', 'yy', '2023-01-07');
INSERT INTO `book_list` VALUES (9, 'zz', 'zz', 'zz', 'zz', '2023-01-18');
INSERT INTO `book_list` VALUES (10, 'aa', 'aa', 'aa', 'aa', '2022-12-30');
INSERT INTO `book_list` VALUES (11, 'bb', 'bb', 'bb', 'bb', '2022-12-26');
INSERT INTO `book_list` VALUES (12, 'cc', 'cc', 'cc', 'cc', '2022-12-29');
*/

namespace LargeFilesSample.Controllers
{
    [Route("book")]
    [ApiController]
    public class BookIndexController : ControllerBase
    {
        //private readonly IServiceProvider _provider;

        // 数据库上下文 dbContext
        private readonly DocumentStorageCenterDbContext _dbContext;

        private readonly ILogger<BookIndexController> _logger;

        public BookIndexController(ILogger<BookIndexController> logger,
            //IServiceProvider provider,
            DocumentStorageCenterDbContext dbContext)
        {
            _logger = logger;
            //_provider = provider;
            _dbContext = dbContext;
        }

        [Route("index")]
        [HttpGet]
        [AllowAnonymous]
        public ActionResult<PageableBookList> IndexByPage(int page=0, int size=5)
        {
            const int MAX_ITEMS_PER_PAGE = 50;
            if (!(page >= 0 && size >= 1 && size <= MAX_ITEMS_PER_PAGE))
            {
                string reason = $"Error: page={page}, size={size}: 页码参数无效! 要求page>=0 && size>=1 && size<={MAX_ITEMS_PER_PAGE}";
                var badResponse = new PageableBookList()
                {
                    Message = reason,
                    Content = new List<Book>(),
                };
                return BadRequest(badResponse);
            }
            var bookRepository = _dbContext.BookRepository;
            // 总计多少条数据
            var totalElements = bookRepository.Count();
            // 总计分为多少页
            int totalPages = totalElements / size;
            // 筛选当前页码
            List<Book> selected = bookRepository.OrderBy(book => book.Id).Skip(page * size).Take(size).ToList();
            // 封装
            var result = new PageableBookList()
            {
                Message = "Success",
                NumberOfElements = selected.Count,
                Content = selected,
                TotalElements = totalElements,
                TotalPages = totalPages,
                CurrentPageNumber = page,
            };
            return Ok(result);
        }
        public class PageableBookList : GenericPageableList<Book>
        {
        }
    }
}
